<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The Vector class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ds-sequence.unshift.html">« Ds\Sequence::unshift</a></li>
      <li style="float: right;"><a href="ds-vector.allocate.html">Ds\Vector::allocate »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.ds.html">Data Structures</a></li>
    <li>The Vector class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.ds-vector" class="reference">

 <h1 class="title">The Vector class</h1>
 

 <div class="partintro"><p class="verinfo">(No version information available, might only be in Git)</p>


  <div class="section" id="ds-vector.intro">
   <h2 class="title">简介</h2>
   <p class="para">
    A Vector is a sequence of values in a contiguous buffer that grows and
    shrinks automatically. It’s the most efficient sequential structure because
    a value’s index is a direct mapping to its index in the buffer, and the
    growth factor isn&#039;t bound to a specific multiple or exponent.
   </p>
  </div>



<div class="section" id="ds-vector.strengths">
    <h2 class="title">Strengths</h2>
    <p class="para">
        <ul class="simplelist">
            <li class="member">Supports array syntax (square brackets).</li>
            <li class="member">Uses less overall memory than an <span class="type">array</span> for the same number of values.</li>
            <li class="member">Automatically frees allocated memory when its size drops low enough.</li>
            <li class="member">Capacity does not have to be a power of 2.</li>
            <li class="member">
                <span class="function"><strong>get()</strong></span>,
                <span class="function"><strong>set()</strong></span>,
                <span class="function"><strong>push()</strong></span>,
                <span class="function"><strong>pop()</strong></span> are all O(1).
            </li>
        </ul>
   </p>
</div>

<div class="section" id="ds-vector.weaknesses">
    <h2 class="title">Weaknesses</h2>
    <p class="para">
        <ul class="simplelist">
            <li class="member">
                <span class="function"><strong>shift()</strong></span>,
                <span class="function"><strong>unshift()</strong></span>,
                <span class="function"><strong>insert()</strong></span> and
                <span class="function"><strong>remove()</strong></span> are all O(n).
            </li>
        </ul>
   </p>
</div>

  <div class="section" id="ds-vector.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass">
        
    </div>


    <div class="classsynopsisinfo">

    <span class="ooclass"><span class="modifier">class</span> <strong class="classname">Ds\Vector</strong></span>
    <span class="oointerface"><span class="modifier">implements</span>  <strong class="interfacename">Ds\Sequence</strong></span><span class="oointerface">,  <a href="class.arrayaccess.html" class="interfacename">ArrayAccess</a></span> {</div>

    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Constants */</div>
    <div class="fieldsynopsis">
     <span class="modifier">const</span>
     <span class="type">int</span>
      <var class="fieldsynopsis_varname"><a href="class.ds-vector.html#ds-vector.constants.min-capacity"><var class="varname">MIN_CAPACITY</var></a></var><span class="initializer"> = 10</span>;</div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* 方法 */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="ds-vector.allocate.html" class="methodname">allocate</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$capacity</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.apply.html" class="methodname">apply</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.capacity.html" class="methodname">capacity</a></span>(): <span class="type">int</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.clear.html" class="methodname">clear</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.contains.html" class="methodname">contains</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.copy.html" class="methodname">copy</a></span>(): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.filter.html" class="methodname">filter</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.find.html" class="methodname">find</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.first.html" class="methodname">first</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.get.html" class="methodname">get</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.insert.html" class="methodname">insert</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.isempty.html" class="methodname">isEmpty</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.join.html" class="methodname">join</a></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$glue</code><span class="initializer"> = ?</span></span>): <span class="type">string</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.last.html" class="methodname">last</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.map.html" class="methodname">map</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.merge.html" class="methodname">merge</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$values</code></span>): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.pop.html" class="methodname">pop</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.push.html" class="methodname">push</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.reduce.html" class="methodname">reduce</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$initial</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.remove.html" class="methodname">remove</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.reverse.html" class="methodname">reverse</a></span>(): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.reversed.html" class="methodname">reversed</a></span>(): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.rotate.html" class="methodname">rotate</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$rotations</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.set.html" class="methodname">set</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.shift.html" class="methodname">shift</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.slice.html" class="methodname">slice</a></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$index</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.sort.html" class="methodname">sort</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.sorted.html" class="methodname">sorted</a></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$comparator</code><span class="initializer"> = ?</span></span>): <span class="type"><a href="class.ds-vector.html" class="type Ds\Vector">Ds\Vector</a></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.sum.html" class="methodname">sum</a></span>(): <span class="type"><span class="type">int</span>|<span class="type">float</span></span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.toarray.html" class="methodname">toArray</a></span>(): <span class="type">array</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="ds-vector.unshift.html" class="methodname">unshift</a></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$values</code><span class="initializer"> = ?</span></span>): <span class="type"><span class="type void">void</span></span></div>

   }</div>


  </div>


  <div class="section" id="ds-vector.constants">
   <h2 class="title">预定义常量</h2>
   <dl>

    
     <dt id="ds-vector.constants.min-capacity"><strong><code>Ds\Vector::MIN_CAPACITY</code></strong></dt>

     <dd>

      <p class="para"/>
     </dd>

    

   </dl>

  </div>


  <div class="section">
   <h2 class="title">更新日志</h2>
   <p class="para">
    <table class="doctable informaltable">
     
      <thead>
       <tr>
        <th>版本</th>
        <th>说明</th>
       </tr>

      </thead>

      <tbody class="tbody">
       <tr>
        <td>PECL ds 1.3.0</td>
        <td>
         The class now implements <span class="classname"><a href="class.arrayaccess.html" class="classname">ArrayAccess</a></span>.
        </td>
       </tr>

      </tbody>
     
    </table>

   </p>
  </div>

 </div>

 















































































































































































































































<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="ds-vector.allocate.html">Ds\Vector::allocate</a> — Allocates enough memory for a required capacity</li><li><a href="ds-vector.apply.html">Ds\Vector::apply</a> — Updates all values by applying a callback function to each value</li><li><a href="ds-vector.capacity.html">Ds\Vector::capacity</a> — Returns the current capacity</li><li><a href="ds-vector.clear.html">Ds\Vector::clear</a> — Removes all values</li><li><a href="ds-vector.construct.html">Ds\Vector::__construct</a> — Creates a new instance</li><li><a href="ds-vector.contains.html">Ds\Vector::contains</a> — Determines if the vector contains given values</li><li><a href="ds-vector.copy.html">Ds\Vector::copy</a> — Returns a shallow copy of the vector</li><li><a href="ds-vector.count.html">Ds\Vector::count</a> — Returns the number of values in the collection</li><li><a href="ds-vector.filter.html">Ds\Vector::filter</a> — Creates a new vector using a callable to
    determine which values to include</li><li><a href="ds-vector.find.html">Ds\Vector::find</a> — Attempts to find a value's index</li><li><a href="ds-vector.first.html">Ds\Vector::first</a> — Returns the first value in the vector</li><li><a href="ds-vector.get.html">Ds\Vector::get</a> — Returns the value at a given index</li><li><a href="ds-vector.insert.html">Ds\Vector::insert</a> — Inserts values at a given index</li><li><a href="ds-vector.isempty.html">Ds\Vector::isEmpty</a> — Returns whether the vector is empty</li><li><a href="ds-vector.join.html">Ds\Vector::join</a> — Joins all values together as a string</li><li><a href="ds-vector.jsonserialize.html">Ds\Vector::jsonSerialize</a> — Returns a representation that can be converted to JSON</li><li><a href="ds-vector.last.html">Ds\Vector::last</a> — Returns the last value</li><li><a href="ds-vector.map.html">Ds\Vector::map</a> — Returns the result of applying a callback to each value</li><li><a href="ds-vector.merge.html">Ds\Vector::merge</a> — Returns the result of adding all given values to the vector</li><li><a href="ds-vector.pop.html">Ds\Vector::pop</a> — Removes and returns the last value</li><li><a href="ds-vector.push.html">Ds\Vector::push</a> — Adds values to the end of the vector</li><li><a href="ds-vector.reduce.html">Ds\Vector::reduce</a> — Reduces the vector to a single value using a callback function</li><li><a href="ds-vector.remove.html">Ds\Vector::remove</a> — Removes and returns a value by index</li><li><a href="ds-vector.reverse.html">Ds\Vector::reverse</a> — Reverses the vector in-place</li><li><a href="ds-vector.reversed.html">Ds\Vector::reversed</a> — Returns a reversed copy</li><li><a href="ds-vector.rotate.html">Ds\Vector::rotate</a> — Rotates the vector by a given number of rotations</li><li><a href="ds-vector.set.html">Ds\Vector::set</a> — Updates a value at a given index</li><li><a href="ds-vector.shift.html">Ds\Vector::shift</a> — Removes and returns the first value</li><li><a href="ds-vector.slice.html">Ds\Vector::slice</a> — Returns a sub-vector of a given range</li><li><a href="ds-vector.sort.html">Ds\Vector::sort</a> — Sorts the vector in-place</li><li><a href="ds-vector.sorted.html">Ds\Vector::sorted</a> — Returns a sorted copy</li><li><a href="ds-vector.sum.html">Ds\Vector::sum</a> — Returns the sum of all values in the vector</li><li><a href="ds-vector.toarray.html">Ds\Vector::toArray</a> — Converts the vector to an array</li><li><a href="ds-vector.unshift.html">Ds\Vector::unshift</a> — Adds values to the front of the vector</li></ul>
</div>
</div></div></body></html>